package com.allwinter.yibangsafe.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.allwinter.yibangsafe.domain.Comment;
import com.allwinter.yibangsafe.service.CommentService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/comment")
@CrossOrigin(origins = "*")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @GetMapping("/allComments")
    @ResponseBody
    public JSONObject allComments(String serviceid, String userid) {
        System.out.println("serviceid: " + serviceid);
        System.out.println("userid: " + userid);
        JSONObject comments = commentService.getComment(Long.parseLong(serviceid), Long.parseLong(userid));
        return comments;
    }

    @GetMapping("saveComment")
    @ResponseBody
    public  JSONObject saveComment(String newComment,String userid,String serviceid) {
        JSONObject res = new JSONObject();
        boolean addRes = commentService.addComment(newComment, userid, serviceid);

        if (addRes) {
            res.put("code",1);
            res.put("msg","saveComment success!");
        } else {
            res.put("code",0);
            res.put("msg","saveComment fail!");
        }

        return res;
    }

    @GetMapping("likeNumUp")
    @ResponseBody
    public JSONObject likeNumUp(String commentid) {
        JSONObject res = new JSONObject();
        System.out.println(commentid);
        boolean UpRes = commentService.likeNumUp(Long.parseLong(commentid));
        if (UpRes) {
            res.put("code",1);
            res.put("msg","likeNumUp success!");
        } else {
            res.put("code",0);
            res.put("msg","likeNumUp fail!");
        }
        return res;
    }




}
